package dao.Impl;

import entity.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public class empchange {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    // 创建员工记录
    public Boolean create(Employee employee) {
        String sql = "INSERT INTO empchange (emp_id, emp_password, emp_name, emp_gender, emp_birth_date, emp_idcard, emp_phone, " +
                "emp_education, emp_role, org_id, emp_entry_date, salary_standard_id, emp_status) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

       return jdbcTemplate.update(sql,
                employee.getEmpId(),
                employee.getEmpPassword(),
                employee.getEmpName(),
                employee.getEmpGender(),
                employee.getEmpBirthDate(),
                employee.getEmpIdcard(),
                employee.getEmpPhone(),
                employee.getEmpEducation(),
                employee.getEmpRole(),
                employee.getOrgId(),
                employee.getEmpEntryDate(),
                employee.getSalaryStandardId(),
                employee.getEmpStatus())>0;
    }

    // 查询所有员工记录
    public List<Employee> findAll() {
        String sql = "SELECT * FROM empchange";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Employee.class));
    }

    // 根据ID查询单个员工记录
    public Employee findById(Long id) {
        String sql = "SELECT * FROM empchange WHERE emp_id = ?";
        return jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<>(Employee.class));
    }

    // 更新员工记录
    public void update(Employee employee) {
        String sql = "UPDATE empchange SET emp_password=?, emp_name=?, emp_gender=?, emp_birth_date=?, emp_idcard=?, " +
                "emp_phone=?, emp_education=?, emp_role=?, org_id=?, emp_entry_date=?, salary_standard_id=?, emp_status=? WHERE emp_id=?";
        jdbcTemplate.update(sql,
                employee.getEmpPassword(),
                employee.getEmpName(),
                employee.getEmpGender(),
                employee.getEmpBirthDate(),
                employee.getEmpIdcard(),
                employee.getEmpPhone(),
                employee.getEmpEducation(),
                employee.getEmpRole(),
                employee.getOrgId(),
                employee.getEmpEntryDate(),
                employee.getSalaryStandardId(),
                employee.getEmpStatus(),
                employee.getEmpId());
    }

    // 删除员工记录
    public Boolean deleteById(Long id) {
        String sql = "DELETE FROM empchange WHERE emp_id = ?";
        return jdbcTemplate.update(sql, id)>0;
    }
}
